﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SHack
{
    class BaseScan
    {
        public delegate void ScanCompleteHandler();
        public event ScanCompleteHandler ScanCompleteEvent;


        protected string _error;
        protected bool _isrun;
        protected int _totalCount;
        protected int _passCount;
        protected int _succCount;

        protected int _timeOut;
        protected int _speed;


        protected object _lockerSucc = new object();
        protected object _lockerPass = new object();
        protected object _lockerComplete = new object();

        protected DateTime _lastTime;


        public int TotalCount
        {
            get { return _totalCount; }
        }

        public int PassCount
        {
            get { return _passCount > _totalCount ? _totalCount : _passCount; }
        }

        public int SuccCount
        {
            get { return _succCount; }
        }

        public int Speed
        {
            get {
                try
                {
                    TimeSpan ts = DateTime.Now - _lastTime;
                    if (ts.TotalSeconds < 0)
                        return _passCount;
                    int second = (int)ts.TotalSeconds;
                    if(second<1)
                        return _passCount;
                    _speed = _passCount  /second ;
                    return _speed;
                }
                catch {
                    return _passCount;
                }

            }
        }

        public int AvgSpeed
        {
            get { return _speed; }
        }

        public string SpandTime
        {
            get {
                TimeSpan ts = DateTime.Now - _lastTime;
                return ts.ToString().Substring(0,8);
            }   
        }

        public string ExpectComplete
        {
            get 
            {
                try
                {
                    if (_speed == 0)
                        return "?";
                    int ticks=((_totalCount - _passCount) / _speed);
                    TimeSpan ts=new TimeSpan(0,0,ticks);
                    return ts.ToString();
                }
                catch
                {
                    return "?";
                }
                
            }
        }
        public string Error
        {
            get { return _error; }
        }

        protected void AddPass()
        {
            lock (_lockerPass)
            {
                _passCount++;
            }
        }

        protected void AddSucc()
        {
            lock (_lockerSucc)
            {
                _succCount++;
            }
        }



        public void CallStop()
        {
            lock (_lockerComplete)
            {
                if (_isrun)
                {
                    _isrun = false;
                    _passCount = _totalCount;
                    if (ScanCompleteEvent != null)
                        ScanCompleteEvent();
                }
                
            }
        
        }

    }
}
